﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SangsTools;
using UnityEngine.SceneManagement;

namespace UI
{
    public class UIDesCanvas : SingletonScript<UIDesCanvas>
    {
        private const string PREFABPATH = "UI/CanvasDes";
        private GameObject objCanvas;
        private UIDesItem desItemPrefab = null;

        public PoolComponent<UIDesItem> poolDesItem { get; protected set; }

        public UIDesCanvas()
        {
            objCanvas = MonoBehaviour.Instantiate(Resources.Load<GameObject>(PREFABPATH));
            MonoBehaviour.DontDestroyOnLoad(objCanvas);
            desItemPrefab = objCanvas.transform.Find("Root/DesItem").GetComponent<UIDesItem>();
            desItemPrefab.transform.localScale = Vector3.zero;

            poolDesItem = new PoolComponent<UIDesItem>(desItemPrefab);

            desItemPrefab.gameObject.SetActive(false);

            SceneManager.activeSceneChanged += OnSceneChange;
        }

        private void OnSceneChange(Scene newScene, Scene oldScene)
        {
            poolDesItem.RecycleAll();
        }

        public UIDesItem ShowDes(string des, Transform target)
        {
            return poolDesItem.GetMember(desItemPrefab.transform.parent).Show(des, target);
        }
        public void RecycleDesItem(UIDesItem item)
        {
            if (item != null)
                poolDesItem.Recycle(item);
        }
    }
}